package com.sbr.seb.wicket.page.delivery;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.sbr.seb.entity.livraison.Application;
import com.sbr.seb.planning.dao.ApplicationDao;
import com.sbr.seb.wicket.menu.delivery.DeliverySideMenu;
import com.sbr.seb.wicket.page.BasePage;

public class DeliveryBasePage extends BasePage {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private ApplicationDao applicationDao;

	private Application selectedApplication;

	public DeliveryBasePage() {
		super();

		Form<Application> monthForm = new Form<Application>("applicationChange");
		monthForm.setDefaultModel(new CompoundPropertyModel<DeliveryBasePage>(this));

		final DropDownChoice<Application> applicationChoice = new DropDownChoice<Application>("selectedApplication", new LoadableDetachableModel<List<Application>>(){
			private static final long serialVersionUID = 1L;

			@Override
			protected List<Application> load() { 
				return applicationDao.findAll();
			}
		},  new ChoiceRenderer<Application>("name", "id")){
			private static final long serialVersionUID = 1L;

			@Override
			protected boolean wantOnSelectionChangedNotifications() {
				return true;
			}

			@Override
			protected void onSelectionChanged(final Application newSelection) {
				logger.debug("onUpdate "+selectedApplication.getName());
				getMySession().setSelectedApplication(newSelection);
				logger.debug("getPage() "+getPage());
				setResponsePage(getPage().getClass());
			}
		};

		//		applicationChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
		//			private static final long serialVersionUID = 1L;
		//
		//			@Override
		//			protected void onUpdate(AjaxRequestTarget target) {
		//				logger.debug("onUpdate "+selectedApplication.getName());
		//				getMySession().setSelectedApplication(selectedApplication);
		//				setResponsePage(getPage());
		//			}
		//
		//		});

		monthForm.add(applicationChoice);

		add(monthForm);



		add(new DeliverySideMenu("deliveryMenu"));
	}

	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}

	public Application getSelectedApplication() {
		return selectedApplication;
	}

	public void setSelectedApplication(Application selectedApplication) {
		this.selectedApplication = selectedApplication;
	}

}
